import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams,ViewController } from 'ionic-angular';
import {FormBuilder, Validators} from '@angular/forms';
import {LoginPage} from '../login/login';
import { Storage } from '@ionic/storage';
import {TabsPage} from "../tabs/tabs";
import {UsersService} from '../../providers/users.service';
import { ToastController } from 'ionic-angular';
@IonicPage()
@Component({
  selector: 'page-regist',
  templateUrl: 'regist.html',
  providers:[UsersService]
})
export class RegistPage {
  registerForm: any;
  username: any;
  password: any;
  idcode:any;
  iTime:any = 59;
  code_res: any;
  reg_res: any;
  num: any;
  code_mes: any = '获取验证码';
  userName:any;
  userId:any;
  flag: any = false;
  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private formBuilder: FormBuilder,
              private viewCtrl: ViewController,
              private userSer:UsersService,
              private storage:Storage,
              private toastCtrl: ToastController,
  ) {
    this.registerForm = this.formBuilder.group({

      telephone: ['', Validators.compose([Validators.minLength(11), Validators.maxLength(11), Validators.required, Validators.pattern("^(13[0-9]|15[012356789]|17[03678]|18[0-9]|14[57])[0-9]{8}$")])],
      password: ['', Validators.compose([Validators.required, Validators.minLength(6)])],
      code: ['', Validators.compose([Validators.required, Validators.minLength(6), Validators.maxLength(6)])],
    });
    this.username = this.registerForm.controls['telephone'];
    this.password = this.registerForm.controls['password'];
    this.idcode = this.registerForm.controls['code'];

  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad RegistPage');
  }

  confirm() {
    this.navCtrl.setRoot(LoginPage)
  }

  dismiss() {
    this.viewCtrl.dismiss();
  }


  register(registerForm){
    this.userSer.register(registerForm).then((result)=> {
      console.log(result);
      if(result){
        if(result.stateCode===6){
          this.storage.ready().then(()=>{
            console.log(result);
            this.storage.set('userName',result.userName);
            this.storage.set('userId',result.userId);
          });
          this.navCtrl.push(TabsPage);

        }else {
          let toast = this.toastCtrl.create({
            message: '用户名或密码错误',
            duration: 3000
          });
          toast.present();
        }
      }else{
        let toast = this.toastCtrl.create({
          message: '服务器异常',
          duration: 3000
        });
        toast.present();
      }
    })

  }

  getCode() {

    this.userSer.getCodeByphone({"telephone":this.username.value}).then ((result)=> {
      console.log(result);
      // if (result.stageCode == 5)
      if (result.stateCode === 5) {

        this.code_res = ' 该手机号已经注册过 ';
        // console.log(this.cdoe_res);
      } else if (result.stateCode == 'e004') {

        this.navCtrl.push(TabsPage);
        console.log(this.code_res);
      } else {
        if (result) {
          // that.num = result.stagteCode;
          // that.code_res = '';
          // console.log(that.num);
          this.RemainTime();
        }
      }
    });

  }

  private timer;

  RemainTime() {
    this.flag = true;
    this.timer = setInterval(() => {
      this.code_mes = this.iTime + '秒后重新获取';
      this.iTime--;
      if (this.iTime < 0) {
        this.flag = false;
        clearInterval(this.timer);
      }
    }, 1000);
  }


}
